//
//  SubjectMoreVC.m
//  AroundTheWorld
//
//  Created by dllo on 16/5/20.
//  Copyright © 2016年 IfelseGo an. All rights reserved.
//

#import "SubjectMoreVC.h"
#import "SubjectMoreCollectionViewCell.h"
#import "DataHandler.h"
#import "SubjectMoreMoedel.h"
#import "SubjectHotVC.h"

@interface SubjectMoreVC ()

@end

@implementation SubjectMoreVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self getData];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH - WIDTH / 26 * 2, (WIDTH - WIDTH / 26 * 2) * 255 / 385);
    flowLayout.sectionInset = UIEdgeInsetsMake(WIDTH / 26, WIDTH / 26, WIDTH / 26, WIDTH / 26);
    flowLayout.minimumLineSpacing = WIDTH / 26;
    UICollectionView *collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 60, WIDTH, HEIGHT - 60) collectionViewLayout:flowLayout];
    
    collectionV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collectionV];
    
    //成为代理人
    collectionV.delegate = self;
    collectionV.dataSource = self;
    
    //注册
    [collectionV registerClass:[SubjectMoreCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    self.myCollectionV = collectionV;
    
    //返回按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(18, 25, 40, 40);
    button.backgroundColor = [UIColor clearColor];
    [button setImage:[UIImage imageNamed:@"btn-fanhui64x64"] forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonAcion) forControlEvents:UIControlEventTouchUpInside];
    
    //title
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 15, WIDTH - 100, 60)];
    label.text = @"热门游记专题";
    label.textColor = [UIColor grayColor];
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
}

//data
-(void)getData{
    self.mySubjectMoreArrayM = [NSMutableArray array];
     DataHandler *handler1 = [[DataHandler alloc] init];
    [handler1 getSubjectMoreNotes:^(id result) {
        self.mySubjectMoreArrayM = [NSMutableArray arrayWithArray:result];
        [self.myCollectionV reloadData];
     } failedHandler:^(NSError *error) {
    }];
}

//cell数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.mySubjectMoreArrayM.count;
}
//建cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SubjectMoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    SubjectMoreMoedel *model = [self.mySubjectMoreArrayM objectAtIndex:indexPath.row];
   [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@""]];
    return cell;
}

//返回
- (void)buttonAcion{
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SubjectMoreMoedel *model = [self.mySubjectMoreArrayM objectAtIndex:indexPath.row];
    SubjectHotVC *VC = [[SubjectHotVC alloc] init];
    VC.myUrl = model.url;
    VC.myTitle = model.title;
    [self presentViewController:VC animated:YES completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
